<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use DB;

use Hash;

use App\Org\Image;

use App\Http\Requests;

use App\Http\Controllers\Controller;

class CenterController extends Controller
{
    /**
     *   个人中心首页
     */
    public function getCenter()
    {
        $uid = session()->get('username')->id;
        $msg = DB::table('project_user')->where('id',$uid)->first();
        $nickname = $msg->nickname;
        $userpic = $msg->picname;
        $username = $msg->username;
        $history = DB::table('project_user_details')->where('uid',$uid)->first();
        $date = explode('-',$history->date);
        $date = $date[0].'年'.$date[1].'月'.$date[2].'日';
        $timenow = $history->time;
        $lastip = $history->lastip;
        $time = explode(':',$history->time);
        $time = $time[0].':'.$time[1];
        $datetime = $date.' '.$time;
        return view('home/center/info/center_index',['history'=>$history],compact('nickname','lastip','timenow','date','datetime','userpic','username','uid'));
    }

    /**
     *   个人中心我喜欢界面
     */
    public function getLove()
    {
        return view('home/center/center_love');
    }

    /**
     *   个人中心地址页面
     */
    public function getAddress()
    {
        return view('home/center/center_address');
    }

    /**
     *   前台订单详情页
     */

    public function getOrderdetailed()
    {
        $ordernum = $_GET['id'];
        $res = DB::table('project_order')->where('ordernum',$ordernum)->first();

        $address = explode(',',trim($res->addre));
        $name = $address[0];
        $phone = $address[2];
        $useraddress = $address[1];
        $goodsname = $res->goodsname;
        $goodspic = $res->goodspic;
        $ordertimes = $res->ordertime;
        $ordertimes = explode(' ',$ordertimes);
        $ordertime = $ordertimes[0];
        $ordermin = $ordertimes[1];
        $orderprice = $res->orderprice;
        $num = $res->num;
        $status = $res->status;
        return view('home/center/center_orderdetailed',compact('name','status','useraddress','phone','goodsname','goodspic','ordertime','ordermin','orderprice','num','ordernum'));
    }

    /**
     *   个人中心进入页面
     */
    public function getOrder()
    {
        $shoplist = session()->get('shoplist');
        foreach ($shoplist as $v){
            $gid = $v->id;
        }
        $goodsmsg = DB::table('project_goods')->where('id',$gid)->first();
        $goodspic = $goodsmsg->goodspic;
        $goodsname = $goodsmsg->goodsname;
        $goodsprice = $goodsmsg->goodsprice;
        $status = isset($_GET['status'])?$_GET['status']:-1;
        $uid = session()->get('username')->id;
        if($status == -1){
            $orderstatus = DB::table('project_order')->where('uid',$uid)->orderby('ordertime','desc')->paginate(1);
        }else{
            $orderstatus = DB::table('project_order')->where('status',$status)->where('uid',$uid)->orderby('ordertime','desc')->paginate(1);
        }

        foreach($orderstatus as $v){
            switch($v->status){
                case 0:
                    $v->status = '待付款';
                    break;
                case 1:
                    $v->status = '待发货';
                    break;
                case 2:
                    $v->status = '待签收';
                    break;
                case 3:
                    $v->status = '已完成';
                    break;
                case 4:
                case 5:
                    $v->status = '已关闭';
                    break;
            }
        }
        return view('home/center/center_order',['orderstatus'=>$orderstatus],compact('status','goodspic','goodsname','goodsprice','gid'));
    }

    /**
     *  换货界面
     */
    public function getReturnlist()
    {
        $goodsstatus = isset($_GET['goodsstatus'])?$_GET['goodsstatus']:1;
        $uid = session()->get('username')->id;
        $list = DB::table('project_order_detail')->where('goodsstatus',$goodsstatus)->where('uid',$uid)->paginate(3);
        foreach($list as $v){
            switch($v->goodsstatus){
                case 1:
                    $v->goodsstatus = '退货中';
                    break;
                case 2:
                    $v->goodsstatus = '换货处理中';
                    break;
            }
        }

        return view('home/center/center_returnlist',['list'=>$list],compact('goodsstatus'));
    }

    /**
     *  取消订单操作
     */
    public function getClose()
    {
        $ordernum = $_GET['ordernum'];
        $res = DB::update('update project_order set status = 4 where ordernum = ?', [$ordernum]);
        if($res){
            echo 'yes';
        }else{
            echo 'no';
        }
    }

    /**
     *  换货操作
     */
    public function getExchange()
    {
        $uid = session()->get('username')->id;
        $ordernum = $_GET['ordernum'];
        $res = DB::table('project_order')->where('ordernum',$ordernum)->first();
        $result = DB::update('update project_order set status = 5 where ordernum = ?', [$ordernum]);
        $changelist['ordernum'] = $ordernum;
        $changelist['goodsname'] = $res->goodsname;
        $changelist['orderprice'] = $res->orderprice;
        $changelist['goodspic'] = $res->goodspic;
        $changelist['num'] = $res->num;
        $changelist['applytime'] = date('Y-m-d H:i:s',time());
        $changelist['goodsstatus'] = 1;
        $changelist['uid'] = $uid;
        $pro = DB::table('project_order_detail')->insert($changelist);
        if($result && $pro){
            echo 'yes';
        }else{
            echo 'no';
        }

    }

    /**
     *  退货操作
     */
    public function getReturngoods()
    {
        $uid = session()->get('username')->id;
        $ordernum = $_GET['ordernum'];
        $res = DB::table('project_order')->where('ordernum',$ordernum)->first();
        $result = DB::update('update project_order set status = 5 where ordernum = ?', [$ordernum]);
        $changelist['ordernum'] = $ordernum;
        $changelist['goodsname'] = $res->goodsname;
        $changelist['orderprice'] = $res->orderprice;
        $changelist['goodspic'] = $res->goodspic;
        $changelist['num'] = $res->num;
        $changelist['applytime'] = date('Y-m-d H:i:s',time());
        $changelist['goodsstatus'] = 2;
        $changelist['uid'] = $uid;
        $pro = DB::table('project_order_detail')->insert($changelist);
        if($result && $pro){
            echo 'yes';
        }else{
            echo 'no';
        }

    }

    /**
     *  确认收货操作
     */
    public function getConfirm()
    {
        $ordernum = $_GET['ordernum'];
        $res = DB::update('update project_order set status = 3 where ordernum = ?', [$ordernum]);
        if($res){
            echo 'yes';
        }else{
            echo 'no';
        }
    }

    /**
     *  个人中心评价界面
     */
    public function getInfoeval()
    {
        $ordernum = isset($_GET['id'])?$_GET['id']:'';
        $goodsorders = DB::table('project_order')->where('ordernum',$ordernum)->first();
        $ordertime = $goodsorders->ordertime;
        $orderprice = $goodsorders->orderprice;
        $uid = $goodsorders->uid;
        $num = $goodsorders->num;
        $gid = $goodsorders->gid;
        $goodsdetails = DB::table('project_goods')->where('id',$gid)->first();
        $goodsname = $goodsdetails->goodsname;
        $goodsprice = $goodsdetails->goodsprice;
        $goodspic = $goodsdetails->goodspic;
        return view('home/center/center_eval',compact('ordertime','orderprice','uid','num','ordernum','goodsprice','goodsname','goodspic','gid'));
    }

    /**
     *  评价插入数据库
     */
    public function postDocomment(Request $request)
    {
        $comment = $request->except('_token');
        $uid = $request->uid;
        $gid = $request->gid;
        $ordernum = DB::table('project_order')->where('uid',$uid)->where('gid',$gid)->first();
        $ordernum = $ordernum->ordernum;
        if($request->hasFile('pic')) {

            //随机号
            $name = rand(10000,99999).time();
            //后缀名
            $suffix = $request->file('pic')->getClientOriginalExtension();

            //图片文件
            $filename = $name.'.'.$suffix;   //执行图片压缩文件

            //存入本地
            $request->file('pic')->move('./upload/',$filename);

            //new对象
//            $img = new Image;
//            //调用方法
//            $img->open('./upload/'.$filename)->thumb(100,100)->save('./upload/s_'.$filename);

            //写入数据库
//            $comment['pic'] = "/upload/s_".$filename;  //压缩后的图片
            $comment['pic'] = "/upload/".$filename;   // 未压缩的图片

        }
        $comment['posttime'] = date('Y-m-d H:i:s',time());
        $res = DB::table('project_goods_comment')->insert($comment);
        if($res){
            DB::update('update project_order set status = 4 where ordernum = ?', [$ordernum]);
            return redirect('/home/order');
        }else{
            return back();
        }

    }

    /**
     *  个人中心信息页帮助页面
     */
    public function getInfohelp()
    {
        return view('home/center/info/center_info_help');
    }

    /**
     *  个人中心安全界面
     */
    public function getInfosafe()
    {
        $uid = session()->get('username')->id;
        return view('home/center/info/center_info_safe');
    }

    /**
     *    个人中心修改头像界面
     */
    public function getInfophoto()
    {
        $uid = session()->get('username')->id;
        $usermsg = DB::table('project_user')->where('id',$uid)->first();
        $userpic = $usermsg->picname;
        return view('home/center/info/center_info_photo',compact('userpic','uid'));
    }

    /**
     *  个人中心 修改头像方法
     */
    public function postChangephoto(Request $request)
    {
        $uid = $_POST['id'];
        $userpic = $request->except('_token','id');
        if($request->hasFile('picname')){
            //随机号
            $name = md5(rand(10000,99999).time());
            //后缀名
            $suffix = $request->file('picname')->getClientOriginalExtension();

            $filename = $name.'.'.$suffix;  //执行压缩的文件图片

            //写入本地
            $request->file('picname')->move('./upload/',$filename);

            //实例化对象
            $img = new Image;

            // 调用方法
            $img->open('./upload/'.$filename)->thumb(100,100)->save('./upload/s_'.$filename);

            //写入数据库
            $userpic['picname'] = "/upload/".$filename;

        }
        $result = DB::table('project_user')->where('id',$uid)->update($userpic);
        if($result){
            return redirect('/home/infophoto')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    /**
     *   个人中心 修改密码
     */
    public function getInfopwd()
    {
        $uid = session()->get('username')->id;
        return view('home/center/info/center_info_password',compact('uid'));
    }

    /**
     *  个人中心验证原始密码ajax
     */
    public function getVerifypassword()
    {
        $password =$_GET['password'];
        $id = $_GET['id'];
        $res = DB::table('project_user')->where('id',$id)->first();
        $oldpassword = $res -> password;
        if(Hash::check($password,$oldpassword)) {
            echo 'yes';
        }else{
            echo 'no';
        }
    }

    /**
     *  个人中心执行修改密码
     */
    public function postChangepwd(Request $request)
    {
        $id = $_POST['id'];
        $pwd = $request->except('_token');
        $newpwd['password'] = Hash::make($pwd['repassword']);
        $res = DB::table('project_user')->where('id',$id)->update($newpwd);
        if($res){
            return redirect('/home/infopwd');
        }else{
            return back();
        }
    }

    /**
     *  个人中心  修改昵称
     */
    public function getNikename()
    {
        $id = session()->get('username')->id;
        $nickname = DB::table('project_user')->where('id',$id)->lists('nickname');
        return view('home/center/info/center_info_nikename',['nickname'=>$nickname],compact('id'));
    }

    /**
     *  个人中心 执行修改昵称
     */
    public function postDonickname(Request $request)
    {
        $id = $_POST['id'];
        $nickname = $request->except('_token','id');
        $res = DB::table('project_user')->where('id',$id)->update($nickname);
        if($res){
            return redirect('/home/center');
        }else{
            return back();
        }
    }

    /**
     *  个人中心历史登录记录
     */
    public function getHistory()
    {
        $uid = session()->get('username')->id;
        $history = DB::table('project_user_details')->where('uid',$uid)->get();
        return view('home/center/info/center_login_history',['history'=>$history]);
    }

    /**
     *  个人中心用户基本资料
     */
    public function getBasicinfo()
    {
        $uid = session()->get('username')->id;
        DB::insert('insert into project_user_details (uid) values (?)', [$uid]);
        $basic_info = DB::table('project_user_details')->where('uid',$uid)->first();
        // 获取所需的个性签名  生日 性别
        $signature = $basic_info->signature;
        $year = $basic_info->year;
        $month = $basic_info->month;
        $day = $basic_info->day;
        // 判断每月有多少天
        switch($month){
            case '1':
            case '3':
            case '5':
            case '7':
            case '8':
            case '12':
                $day_one= '31';
                break;
            case '4':
            case '6':
            case '9':
            case '10':
            case '11':
                $day_one = '30';
                break;
        }
//        if($year % 4 == 0 && $month == 2){
//            $day_one = '28';
//        }else{
//            $day_one = '28';
//        }
        $sex = $basic_info->sex;
        return view('home/center/info/center_basic_info',compact('signature','year','month','day','sex','day_one','uid'));
    }

    /**
     *  修改个人中心基本资料
     */
    public function postDobasicinfo(Request $request)
    {
        $uid = $_POST['uid'];
        $id = DB::table('project_user_details')->where('uid',$uid)->lists('id');
        $basic = $request->except('_token','uid');
        $basic_insert = $request->except('_token');
        if(empty($id)){
            $res = DB::table('project_user_details')->where('uid',$uid)->insert($basic_insert);
            if($res){
                return redirect('/home/basicinfo')->with('success','添加成功');
            }else{
                return back()->with('error','添加失败');
            }
        } else{
            $res = DB::table('project_user_details')->where('uid',$uid)->update($basic);
            if($res){
                return redirect('/home/basicinfo')->with('success','修改成功');
            }else{
                return back()->with('error','修改失败');
            }
        }
    }

    /**
     *  获取个人中心用户详细资料
     */
    public function getDetailedinfo()
    {
        $uid =session()->get('username')->id;
        DB::insert('insert into project_user_detaileds (uid) values (?)', [$uid]);
        $detaileds_info = DB::table('project_user_detaileds')->where('uid',$uid)->first();
        $bloodtype = $detaileds_info->bloodtype;
        $bodytype = $detaileds_info->bodytype;
        $height = $detaileds_info->height;
        $married = $detaileds_info->married;
        $education = $detaileds_info->education;
        $school = $detaileds_info->school;
        $job = $detaileds_info->job;
        $income = $detaileds_info->income;
        $province = $detaileds_info->province;
        $address = $detaileds_info->address;
        $qq = $detaileds_info->qq;
        return view('home/center/info/center_detailed_info',compact('bloodtype','uid','bodytype','height','married','education','school','job','income','province','address','qq'));
    }

    /**
     *  修改个人中心详细资料
     */
    public function postLikeme(Request $request)
    {
        $uid = $_POST['uid'];
        $id = DB::table('project_user_detaileds')->where('uid',$uid)->lists('id');
        $detaileds_msg = $request->except('_token','interest');
        $detaileds = $request->except('_token','uid','interest');
        if(empty($id)){
            $res = DB::table('project_user_detaileds')->where('uid',$uid)->insert($detaileds_msg);
            if($res){
                return redirect('/home/detailedinfo')->with('success','添加成功');
            }else{
                return back()->with('error','添加失败');
            }
        } else{
            $res = DB::table('project_user_detaileds')->where('uid',$uid)->update($detaileds);
            if($res){
                return redirect('/home/detailedinfo')->with('success','修改成功');
            }else{
                return back()->with('error','修改失败');
            }
        }
    }
    /**
     *  支付页面
     */
    public function getPay()
    {
        $ordernum = $_GET['id'];
        $ordermsg = DB::table('project_order')->where('ordernum',$ordernum)->first();
        $orderprice = $ordermsg->orderprice;
        return view('home/center/center_pay',compact('ordernum','orderprice'));
    }

    /**
     *  确认支付界面
     */
    public function getGopay()
    {
        $ordernum = $_GET['ordernum'];
        $res = DB::update('update project_order set status = 1 where ordernum = ?', [$ordernum]);
        if($res){
            echo 'yes';
        }else{
            echo 'no';
        }
    }

    /**
     *  方便上台 改变发货状态
     */
    public function getOverorder()
    {
        $ordernum = $_GET['ordernum'];
        $res = DB::update('update project_order set status = 2 where ordernum = ?', [$ordernum]);
        if($res){
            echo 'yes';
        }else{
            echo 'no';
        }
    }






}
